"use strict";
/* Controllers */
app.controller("traceUserListController", ['$scope', '$location', '$rootScope', '$routeParams', 'traceUserListService', function ($scope, $location, $rootScope, $routeParams, traceUserListService) {

    var vm = $scope.vm;
    vm.p = vm.result={};
    vm.p.userId='80922743629607';
    vm.p.startTime = hll2StdTime(getHBefore(47) + "0000");
    vm.p.endTime = hll2StdTime(getHMBefore(-1)+"00");
    vm.p.hasExp = false;

    $('.date').datetimepicker({
        locale: 'zh-cn',
        format: 'YYYY-MM-DD HH:mm:00'
    });
    $('.date').on('dp.hide', function (e) {
        var currentInput = $(e.currentTarget).find('input')
        var eName = currentInput.attr('name')
        var p = $scope.vm.p
        p[eName] = currentInput.val()
        $scope.$apply()
    })

    $("#startTime").on("dp.change", function (e) {
        $('#endTime').data("DateTimePicker").minDate(e.date);
    });
    $("#endTime").on("dp.change", function (e) {
        $('#startTime').data("DateTimePicker").maxDate(e.date);
    });

    var errorFn = function (result) {
        alert(result);
    };

    $scope.findTraceUser = function () {
        var vm = $scope.vm;
        vm.result.ms = '...';
        var params = {
            ts1: vm.p.startTime,
            ts2: vm.p.endTime,
            userId: vm.p.userId,
            hasExp: vm.p.hasExp
        };
        $('#list_table').bootstrapTable('showLoading');
        $("#btn").addClass('disabled');
        var procFn = function (response) {
            vm.result =  response;
            if (vm.result.data == null) {
                alert("请求失败：" + response.message)
            }else {
                $('#list_table').bootstrapTable('load', vm.result.data);
            }
            $('#list_table').bootstrapTable('hideLoading');
            $("#btn").removeClass('disabled');
        };
        traceUserListService.findTraceUser(params, procFn, errorFn);
    };


//    $scope.queryTraceUserTags = function() {
//        var vm = $scope.vm;
//        var params = {
//            userId: vm.p.userId
//        };
//        var procFn = function (res) {
//            if (res.data == null) {
//                alert("请求失败：" + res.message)
//                return;
//            }
//            vm.p.userName = res.data.userName;
//        };
//        traceUserListService.queryTraceUserTags(params, procFn, errorFn);
//    };

    $scope.queryList = function () {
//        $('#list_table').bootstrapTable('refresh');
        var vm = $scope.vm;
        var d1 = new Date(vm.p.startTime);
        var d2 = new Date(vm.p.endTime);
        var diff = parseInt(d2 - d1) / 1000 / 60 / 60;
        if(diff>90) {
            alert("时间范围超过3天，请保持在3天内");
            return;
        }
        $scope.findTraceUser();
//        $scope.queryTraceUserTags();
    };

    $scope.initializeController = function () {
        var $resultTable = $('#list_table');
        $resultTable.bootstrapTable({
             pageList: [30, 100, 1000], pageSize: 30
        });

        $resultTable.bootstrapTable('hideColumn', 'adDispType');
        $resultTable.bootstrapTable('hideColumn', 'slot');
        $resultTable.bootstrapTable('hideColumn', 'tjfrom');
    };
    $scope.initializeController();

}]);

/* Services */
app.service('traceUserListService', ['ajaxService', function (ajaxService) {

    this.findTraceUser = function (params, successFunction, errorFunction) {
        ajaxService.AjaxPost(params, "/star/api/basic-service/v1/user_trace/findUserTrace.do", successFunction, errorFunction);
    };

//    this.queryTraceUserTags = function (params, successFunction, errorFunction) {
//        ajaxService.AjaxPost(params, "/star/api/basic-service/v1/user_trace/queryTraceUserTags.do", successFunction, errorFunction);
//    };
}]);




function adIdFmt(value, row, index) {
    if (value == null) {
        return "-";
    }
    return [
        row.title, '(',value,')'
    ].join('');
}

function platformFmt(value, row, index) {
    if (value == null) {
        return "-";
    }
    var biz = '其他';
    if(row.biz=='ganji') {
        biz = '赶集';
    }else if(row.biz=='58'){
        biz='58';
    }
    return [
        biz, value
    ].join('');
}